﻿(function (window) {
	window.m = {};
	m.fix = {};
	m.fix.util = {
		showError: function (err) {
			m.fix.message.stop(err);
		}
	};
	/* message begin */
	m.fix.message = {
		pageMessage: "#page-message",
		hide: function () {
			$(m.fix.message.pageMessage).html("");
		},
		info: function (msg) {
			m.fix.message.msg(msg, "info");
		},
		warning: function (msg) {
			m.fix.message.msg(msg, "warning");
		},
		stop: function (msg) {
			m.fix.message.msg(msg, "danger");
		},
		success: function (msg) {
			m.fix.message.msg(msg, "success");
		},
		msg: function (msg, type) {
			m.fix.message.hide();
			var ms = $("<div role=\"alert\" class=\"alert alert-" + type + " alert-dismissible\">");
			ms.html("<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>" + msg);
			ms.appendTo($(m.fix.message.pageMessage));
			ms.pin({ padding: { top: 70 } });
			$(".pin-wrapper").css("height", "auto");
		}
	},
	/* message end */
	/* webapi begin */
	m.fix.webapi = {
		handleRequestError: function () {
			m.fix.util.showError("服务器好像去打酱油了...稍后刷新重试~非常抱歉哇！");
		},
		/* async mode */
		async: function (verb, api, value, key, callback, errCallback) {
			var url = "/Services/WebApi/";
			var data = new Object();
			data.api = api;
			switch (verb.toLowerCase()) {
				case "get":
					data.value = (isUndefined(value) ? "" : value);
					data.key = (isUndefined(key) ? "" : key);
					url += "Get";
					break;
				case "post":
					data.value = JSON.stringify(value);
					url += "Add";
					break;
				case "put":
					data.value = JSON.stringify(value);
					url += "Edit";
					break;
				case "delete":
					data.value = (isUndefined(value) ? "" : value);
					url += "Delete";
					break;
				default:
					m.fix.util.showError("未能识别的请求动作！请求终止！");
					return;
			}
			$.ajax({
				type: "post",
				url: url,
				async: true,
				dataType: "json",
				data: data,
				success: function (data, textStatus) {
					if (data.Result) {
						if (!isUndefined(callback))
							callback.call(this, data.UserData);
					} else {
						if (!isUndefined(errCallback))
							errCallback.call(this, data.UserData);
						else
							m.fix.util.showError(data.UserData);
					}
				},
				error: function () {
					m.fix.webapi.handleRequestError();
				}
			});
		},
		/* get:
		*	GET api/DemoEntity							valueString=""			valueKey=""
		*	GET api/DemoEntity?filter={filter}			valueString="sth"		valueKey="sth"
		*	GET api/DemoEntity/{id}						valueString="sth"		valueKey=""
		*/
		get: function (api, valueString, valueKey, callback, errCallback) {
			m.fix.webapi.async("get", api, valueString, valueKey, callback, errCallback);
		},
		/* post:
		*	POST api/DemoEntity		valueObject
		*/
		add: function (api, valueObject, callback, errCallback) {
			m.fix.webapi.async("post", api, valueObject, "", callback, errCallback);
		},
		/* put:
		*	PUT api/DemoEntity		valueObject
		*/
		edit: function (api, valueObject, callback, errCallback) {
			m.fix.webapi.async("put", api, valueObject, "", callback, errCallback);
		},
		/* delete:
		*	DELETE api/DemoEntity/{id}		valueObject
		*/
		remove: function (api, id, callback, errCallback) {
			m.fix.webapi.async("delete", api, id, "", callback, errCallback);
		}
	};
	m.fix.ajax = {
		async: function (tag, flag, data, callback, errCallback) {
			m.fix.ajax.req(true, tag, flag, data, callback, errCallback);
		},
		sync: function (tag, flag, data, callback, errCallback) {
			m.fix.ajax.req(false, tag, flag, data, callback, errCallback);
		},
		req: function (async, tag, flag, data, callback, errCallback) {
			$.ajax({
				type: "post",
				url: "/Service/" + tag + "Service/" + flag,
				async: async,
				dataType: "json",
				data: data,
				success: function (data, textStatus) {
					if (data.Result) {
						if (!isUndefined(callback))
							callback.call(this, data.UserData);
					} else {
						if (!isUndefined(errCallback))
							errCallback.call(this, data.UserData);
						else
							m.fix.util.showError(data.UserData);
					}
				},
				error: function () {
					m.fix.webapi.handleRequestError();
				}
			});
		}
	};
	/* webapi end */
	/* sidebar begin */
	m.fix.sidebar = {
		init: function () {
			/* find heading*/
			var heading = $("#mysoft-side-nav-" + m.fix.controller + " a");
			if (heading.length <= 0) return;
			heading.attr("aria-expanded", "true");
			heading.removeClass("collapsed");
			/* find items */
			var items = $("#mysoft-side-nav-" + m.fix.controller + "-items");
			items.addClass("in");
			/* find item*/
			var item = $("#mysoft-side-nav-" + m.fix.controller + "-" + m.fix.action);
			item.addClass("active");
		}
	};
	/* sidebar end*/

	/* page action begin*/
	m.fix.page = {
		init: function (location, controller, action) {
			if (isUndefined(location) || isUndefined(controller) || isUndefined(action)) {
				alert("页面参数不完整，请刷新页面后重试！");
				return;
			};
			m.fix.location = location.toLowerCase();
			m.fix.controller = controller.toLowerCase();
			m.fix.action = action.toLowerCase();
			/* init sidebar */
			m.fix.sidebar.init();
		}
	};
	/* page action end*/
})(window)

$(document).ready(function () {

});